package neu.edu.cn.nuemall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import neu.edu.cn.nuemall.entity.ShippingAddress;
import neu.edu.cn.nuemall.mapper.ShippingAddressMapper;
import neu.edu.cn.nuemall.service.AddressService;
import neu.edu.cn.nuemall.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private ShippingAddressMapper shippingAddressMapper;

    @Autowired
    private UserinfoService userinfoService;

    @Override
    public PageInfo<ShippingAddress> getAddressList(Integer page, Integer count, String user) {
        PageHelper.startPage(page,count);
        QueryWrapper<ShippingAddress> addressWrapper=new QueryWrapper<ShippingAddress>();
        addressWrapper.eq("user_Id",user);
        List<ShippingAddress> shippingAddressList=shippingAddressMapper.selectList(addressWrapper);
        PageInfo<ShippingAddress> shippingAddressPageInfo=new PageInfo<ShippingAddress>(shippingAddressList);

        return shippingAddressPageInfo;
    }
    @Override
    public int insertAddress(ShippingAddress address) {
        int i=shippingAddressMapper.insert(address);
        return i;
    }

    @Override
    public int updateAddress(ShippingAddress address) {
        int i=shippingAddressMapper.updateById(address);
        return i;
    }

    @Override
    public int delectAddress(ShippingAddress address) {
        int i=shippingAddressMapper.deleteById(address);
        return i;
    }

    @Override
    public List<ShippingAddress> getAllAddressList() {
        String userId = userinfoService.getCurrentUser();
        QueryWrapper<ShippingAddress> queryWrapper = new QueryWrapper<ShippingAddress>();
        queryWrapper.eq("user_Id", userId);
        List<ShippingAddress> shippingAddresses = shippingAddressMapper.selectList(queryWrapper);
        return shippingAddresses;
    }

}
